# -*- coding: utf-8 -*-
# @Time : 2022/2/7 11:46
from Common.mysql import db
from Common.public_method import utc_8
from Common.yaml_method import YamlMethod
from src.ApiTest.ProjectConfig.Database.mysqlConfig_database import MysqlConfig


class AddMysqlInfo:
    """
    新增mysql信息
    """
    @staticmethod
    def add_mysql_info(database_type, database_name, config_name, project_id, project_name, environment, host, port, user, password,
                       create_user, is_jump=None, jump_port=None, jump_host=None, jump_name=None):
        """
        新增mysql信息
        :param database_type: 数据库类型
        :param database_name: 数据库名
        :param config_name: 配置名称
        :param project_id: 项目ID
        :param project_name: 项目名称
        :param environment: 运行环境
        :param host: 数据库URL
        :param port: 端口号
        :param user: 用户名
        :param password: 密码
        :param create_user: 创建人
        :param is_jump: 是否需要跳板机
        :param jump_port: 跳板机端口
        :param jump_host: 跳板机Ip
        :param jump_name: 跳板机用户名
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        info = MysqlConfig.query.filter_by(projectId=project_id, databaseType=database_type,
                                           environment=environment).first()
        if info is None:
            create_time = utc_8()
            # 项目信息插入数据库
            info = MysqlConfig(databaseType=database_type, databaseName=database_name, configName=config_name, projectId=project_id,
                               projectName=project_name, environment=environment, host=host, port=port, user=user,
                               password=password, create_time=create_time, create_user=create_user, isJump=is_jump,
                               jumpPort=jump_port, jumpHost=jump_host, jumpName=jump_name)
            db.session.add(info)
            db.session.commit()

            res = {
                'code': code[0],
                'message': 'success',
                'data': {}
            }
            return res
        else:
            res = {
                'code': code[1],
                'message': '配置已存在',
                'data': {}
            }
            return res
